package com.idanchuang.component.shadow.route.common.conf;

import com.idanchuang.component.core.config.EnvConfigBean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.util.Assert;

/**
 * Shadow Route Properties
 *
 * @author Raistlin
 */
public class ShadowRouteProperties implements InitializingBean, ApplicationContextAware {

    private String actualDataSourceName;

    private String shadowDataSourceName;

    private boolean useSharding = false;

    private String actualDataNodes;

    private String shadowDataNodes;

    private ApplicationContext context;

    public String getActualDataSourceName() {
        return actualDataSourceName;
    }

    public String getShadowDataSourceName() {
        return shadowDataSourceName;
    }

    public void setActualDataSourceName(String actualDataSourceName) {
        this.actualDataSourceName = actualDataSourceName;
    }

    public void setShadowDataSourceName(String shadowDataSourceName) {
        this.shadowDataSourceName = shadowDataSourceName;
    }

    public boolean isUseSharding() {
        return useSharding;
    }

    public void setUseSharding(boolean useSharding) {
        this.useSharding = useSharding;
    }

    public String getActualDataNodes() {
        return actualDataNodes;
    }

    public void setActualDataNodes(String actualDataNodes) {
        this.actualDataNodes = actualDataNodes;
    }

    public String getShadowDataNodes() {
        return shadowDataNodes;
    }

    public void setShadowDataNodes(String shadowDataNodes) {
        this.shadowDataNodes = shadowDataNodes;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context = applicationContext;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        Assert.hasText(getActualDataSourceName(), "actualDataSourceName can not be empty!");
        Assert.hasText(getShadowDataSourceName(), "shadowDataSourceName can not be empty!");
        if (isUseSharding()) {
            Assert.hasText(getActualDataNodes(), "actualDataNodes can not be empty!");
            Assert.hasText(getShadowDataNodes(), "shadowDataNodes can not be empty!");
        }
        Assert.notNull(context, "spring application context must not null!");
        if(!context.getBean(EnvConfigBean.class).isDisabledMysql()){
            throw new IllegalArgumentException("shadow table can not use with shadow database for mysql!");
        }
    }

}
